#include <example_interfaces/srv/add_two_ints.hpp>
#include <memory>
#include <rclcpp/rclcpp.hpp>
#include <string>

class ServiceServer01 : public rclcpp::Node {
public:
  explicit ServiceServer01(const std::string &name) : Node(name) {
    RCLCPP_INFO(this->get_logger(), "节点已启动：%s", name.c_str());

    add_two_ints_service_ =
        this->create_service<example_interfaces::srv::AddTwoInts>(
            "add_two_ints_srv",
            std::bind(&ServiceServer01::handle_add_two_ints, this,
                      std::placeholders::_1, std::placeholders::_2));
  }

private:
  rclcpp::Service<example_interfaces::srv::AddTwoInts>::SharedPtr
      add_two_ints_service_;

  void handle_add_two_ints(
      const std::shared_ptr<example_interfaces::srv::AddTwoInts::Request>
          request,
      std::shared_ptr<example_interfaces::srv::AddTwoInts::Response> response) {
    response->sum = request->a + request->b;
    RCLCPP_INFO(this->get_logger(), "收到请求: %ld + %ld = %ld", request->a,
                request->b, response->sum);
  }
};

int main(int argc, char **argv) {
  rclcpp::init(argc, argv);
  auto node = std::make_shared<ServiceServer01>("service_server_01");
  rclcpp::spin(node);
  rclcpp::shutdown();
  return 0;
}
